// pages/setManager/setManager.js
let app = getApp()
let request = require('../../utils/request')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageAllHeight: app.globalData.pageAllHeight,
    managerId: 0,
    managerInfo: {},
    nameVal: '',
    phoneVal: '',
    parkId: 0,
    totalParkSpaceVal: '',
    reserveParkSpaceVal: '',
    chargingParkSpaceVal: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('店铺信息：', options)
    this.setData({
      shopId: options.shopId,
      managerId: options.managerId,
      nameVal: options.name,
      phoneVal: options.tel
    })
    // this.getManagerInfo()
  },
  /**
   * 获取姓名
   */
  getNameVal(e) {
    let val = e.detail.value
    this.setData({
      nameVal: val
    })
  },
  /**
   * 获取泊位总数
   */
  getPhoneVal(e) {
    let val = e.detail.value
    if (val.length >= 11) {
      val = val.slice(0,11)
    }
    this.setData({
      phoneVal: val
    })
  },
  /**
   * 获取可约泊位
   */
  getTotalParkSpaceVal(e) {
    let val = e.detail.value
    this.setData({
      totalParkSpaceVal: val
    })
  },
  /**
   * 获取充电桩
   */
  getReserveParkSpaceVal(e) {
    let val = e.detail.value
    this.setData({
      reserveParkSpaceVal: val
    })
  },
  /**
   * 获取手机号
   */
  getChargingParkSpaceVal(e) {
    let val = e.detail.value
    this.setData({
      chargingParkSpaceVal: val
    })
  },
  /**
   * 设置用户信息
   */
  setManagerInfo() {
    let that = this
    let moblieReg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
    if (!moblieReg.test( this.data.phoneVal)) {
      wx.showToast({
        title: '请您填写正确手机号',
        icon: 'none'
      })
      return
    }
    if (this.data.managerId == 0) {
      this.addManagerInfo()
      return
    }
    let parmas = {
      id: this.data.managerId,
      username: this.data.nameVal,
      telephone: this.data.phoneVal
    }
    if (parmas.username.length > 10) {
      wx.showToast({
        title: '管理员名称不能超过10位',
        icon: 'none'
      })
      return
    }
    request.http('/shop/shop-manager/update', 'POST', parmas, 0)
      .then(res => {
        let data = res.data
        if (data.code == 200) {
          wx.showToast({ title: '修改成功' })

          wx.navigateBack({ delta: 1 })
        }
      })
  },
  /**
   * 添加用户信息
   */
  addManagerInfo() {
    let that = this
    let parmas = {
      username: this.data.nameVal,
      telephone: this.data.phoneVal,
      shopId: this.data.shopId
    }
    if (parmas.username.length > 10) {
      wx.showToast({
        title: '管理员名称不能超过10位',
        icon: 'none'
      })
      return
    }
    if(!parmas.username || !parmas.telephone) {
      return
    }
    request.http('/shop/shop-manager/add', 'POST', parmas, 0)
      .then(res => {
        let data = res.data
        if (data.code == 200) {
          wx.showToast({ title: '添加成功' })

          wx.navigateBack({ delta: 1 })
        }
      })
  },
})